<%@page contentType="text/html; charset=UTF-8" %>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/easyui/themes/myself/easyui.css" />
        <link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/easyui/themes/icon.css" />
        <%@ include file="../common/taglibs.jsp" %>
        <%@ include file="../common/meta.jsp" %>
        <script>
            $(function(){
                $("#dictTable").datagrid({
                    pagination: true, //分页显示 
                    loadMsg: "正在加載...",
                    rownumbers: true,
                    fitColumns: true,
                    fit: true,
                    height: 'auto',
                    singleSelect: true,
                    url: './list.action',
                    columns: [[{
                        field: 'id',
                        hidden: true
                    },{
                        field: 'category',
                        title: '类别名称',
                        width: 120,
                        sortable: true
                    }, {
                        field: 'optionText',
                        title: '名称',
                        width: 100,
                        sortable: true
                    }, {
                        field: 'optionValue',
                        title: '值',
                        width: 80,
                        sortable: true
                    }, {
                        field: 'sort',
                        title: '排序',
                        width: 40,
                        align: 'center',
                        sortable: true
                    },{
                        field: 'operation',
                        title: '操作',
                        width: 60,
                        align: 'center',
                        formatter: function(value,row,index) {
                            var url;
                            url = '<a href="#" onclick="editDict('+ row.id +')" >修改</a>' + '&nbsp;&nbsp;' +
                            	'<a href="#" onclick="delDict('+ row.id +')" >删除</a>';
                        	return url;
                        }
                    }]],
                    toolbar: [//工具栏上定义按钮 
                    {
                        id: 'btnadd',
                        text: '新增',
                        iconCls: 'icon-add',
                        handler: function(){
                            openNewWindows();
                        }
                    }]
                
                });
                
                $('#dictCategory').combobox({   
                    url: './get.action?category=DICTIONARY_CATEGORY',   
                    valueField:'optionValue',   
                    textField:'optionValue'  
                });  
            });

            function editDict(id) {
            	parent.$("#tc").tabs('getSelected').find('#content').attr('src','./system/dict/edit.action?operate=update&dict.id='+id);
            }

            function delDict(id) {
    		    $.messager.confirm("提示信息","确认删除此条数据?",function(data) {
        		    if(data) {
        		    	$.messager.progress();
        		    	
        		    	$.ajax({
        					url: "delete.action?dict.id=" + id,		
        					success: function(data) {
        						$.messager.progress('close');
        						$.messager.alert('消息','删除成功!','info');
        						queryData();
        					},
        					error: function() {
        						$.messager.progress('close');
        						$.messager.alert('错误','删除出错,请重试!','error');
            				}
        				});
                	}
    			});
            }
            function queryData(){
                $('#dictTable').datagrid({
                    queryParams: {
                        "criteria.category": $("#dictCategory").combobox('getValue')
                    },
                    sortName: 'category desc, optionText'
                }).datagrid('load');
            }
            
            
            //将数据转化为 datagrid 数据 格式 
            function xorderListToGridData(msg){
                var encoded = $.toJSON(msg)
                var griddata = $.evalJSON(encoded);
                return griddata;
            }
            
            //add 事件 
            function openNewWindows(){
            	parent.$("#tc").tabs('getSelected').find('#content').attr('src','./system/dict/edit.action?operate=insert');
            }
            
            
            //Ok 事件 
            function addStudentMessage(){
                //得到表格中的数据 
                var stuData = $("#studentTable").datagrid("getData");
                var ids = $("#item_id").val();
                
                $.ajax({
                    type: 'post',
                    url: './save.action',
                    data: {
                        "data.itemName": $("#item_nm").val(),
                        "data.itemCd": $("#item_cd").val(),
                        "data.itemId": $("#item_id").val()
                    },
                    success: function(msg){
                        var griddata = xorderListToGridData(msg);
                        var data = griddata.data;
                        if (griddata.success) {
                            if (ids == '') {
                                stuData.total++;
                                //加载行 
                                stuData.rows.push(data);
                                $("#studentTable").datagrid("loadData", stuData);
                            }
                            else {
                                for (var i = 0; i < stuData.rows.length; i++) {
                                    //如果 表格中的Id 和要进行修改的Id 相同的话 就进行修改 
                                    if (stuData.rows[i].id == ids) {
                                        $('#studentTable').datagrid('updateRow', {
                                            index: i,
                                            row: data
                                        });
                                    }
                                }
                            }
                            $('#dlg').dialog('close');
                        }
                        else {
                            $.messager.show({
                                title: 'Error',
                                msg: griddata.actionMessages[0]
                            });
                        }
                    }
                });
            }
            
            //del 事件 
            function delStudent(){
            
                //得到所有的数据 
                var stuData = $("#studentTable").datagrid("getData");
                //得到选中的数据 
                var rows = $("#studentTable").datagrid("getSelections");
                if (rows.length == 0) {
                    $.messager.alert("系统提示", "请选择要删除的行！", 'info');
                    return;
                }
                
                if (confirm('确定删除 这' + rows.length + '条记录?')) {
                    var ids = [];
                    for (var i = 0; i < rows.length; i++) {
                        for (var j = 0; j < stuData.rows.length; j++) {
                            if (stuData.rows[j].id == rows[i].id) {
                                ids.push(rows[i].id);
                                //$("#studentTable").datagrid("deleteRow", j);
                            }
                        }
                    }
                    
                    $.ajax({
                        type: 'post',
                        url: './delete.action',
                        data: {
                            ids: ids.join(',')
                        },
                        success: function(msg){
                            var griddata = xorderListToGridData(msg);
                            if (griddata.success) {
                                $.messager.alert('Info', 'Delete successfully!');
                                $("#studentTable").datagrid("load");
                            }
                            else {
                                $.messager.show({
                                    title: 'Error',
                                    msg: griddata.actionMessages[0]
                                });
                            }
                        }
                    });
                }
            }
            
            //edit 事件 
            function openEditStutable(){
                var rows = $("#studentTable").datagrid('getSelections');
                if (rows == 0) {
                    $.messager.alert("系统提示", "请选择要编辑的行！", 'info');
                    return;
                }
                
                if (rows.length > 1) {
                    $.messager.alert("系统提示", "对不起，该操作只能选择一行！", 'error');
                    return;
                }
                
                for (var i = 0; i < rows.length; i++) {
                    if (rows[i].id != "") {
                        $("#item_id").val(rows[i].itemId);
                        $("#item_nm").val(rows[i].itemName);
                        $("#item_cd").val(rows[i].itemCd);
                    }
                }
                $("#dlg").dialog('open').dialog('setTitle', 'Edit User');
            }
        </script>
        <style>
.tb_input {
	MARGIN: 5px;
	WIDTH: 100%;
}

</style>
    </head>
    <body class="easyui-layout" style="background:#0B5F3D;">
        <div data-options="region:'north',border:false" style="padding:5px">
        	<form id="dictForm" method="post">
            <table border="0" align="center" cellpadding="3" cellspacing="1" class="tb_input">
                <tr>
                    <td class="td_title" nowrap="nowrap" width="150px">
                        数据字典类型
                    </td>
                    <td >
                    	<input type="text" id="dictCategory" name="criteria.category" value="${criteria.category}" style="width:200px;" class="easyui-combobox" />
                    </td>
                    
                    <td>
                        <a href="#" class="easyui-linkbutton" onclick="queryData();" style="width: 50px">查询</a>
                        <a href="#" class="easyui-linkbutton" onclick="$('#dictForm').form('clear');" style="width: 50px">重置</a>
                    </td>
                </tr>
            </table>
            </form>
        </div>
        <div data-options="region:'center',border:false">
            <table id="dictTable">
            </table>
        </div>
    </body>
</html>
